﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Downloader
{
    class FtpUtil
    {
        internal static bool _checkUrl(string url)
        {
            return false;
        }
        internal static long  getFtpFilelength(string url,string uid="Anonymous",string pwd="")
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);
            request.Method = WebRequestMethods.Ftp.GetFileSize;
            request.UseBinary = true;
            request.Credentials = new NetworkCredential(uid, pwd);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            long len =  response.ContentLength;
            response.Close();
            return len;
        }

        internal static byte[] getFtpFileSlice(string url, long a, long b, string uid = "Anonymous", string pwd = "")
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.UseBinary = true;
            request.Credentials = new NetworkCredential(uid, pwd);
            request.ContentOffset = a;
            request.ContentLength = (int)(b - a + 1);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            BinaryReader sr = new BinaryReader(response.GetResponseStream());
            byte[] result = sr.ReadBytes((int)(b-a+1));
            response.Close();
            return result;
        }
    }

}
